package com.cykj.service;


import com.cykj.dto.ResponseDTO;
import com.cykj.mapper.GoodMapper;
import com.cykj.pojo2.CyGood;
import com.cykj.pojo.Good;
import com.cykj.pojo.GoodClass;
import com.cykj.pojo.GoodClassType;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class GoodServiceImpl implements GoodService{
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private GoodMapper goodMapper;

    @PostConstruct
    public void initHotGoods() {
        List<Good> hotGoods = goodMapper.getAllGoods();
        for (Good hotGood : hotGoods) {
            redisTemplate.opsForValue().set("hotGood:" + hotGood.getId(), hotGood, 60, TimeUnit.MINUTES);
            // redisTemplate.opsForValue().set("stock:"+hotGood.getId(),hotGood.getGoodStock(),10,TimeUnit.SECONDS);
            redisTemplate.opsForList().leftPush("hotList", hotGood);
        }
        redisTemplate.expire("hotList", 60, TimeUnit.MINUTES);

}
    @Override
    public ResponseDTO getHotGoods() throws JsonProcessingException {
//        List<CyGood> hotGoods=goodMapper.getHotGoods();
//        for (CyGood hotGood : hotGoods) {
//            redisTemplate.opsForValue().set("hotGood:"+ hotGood.getId(),hotGood,10, TimeUnit.SECONDS);
//            redisTemplate.opsForValue().set("stock:"+hotGood.getId(),hotGood.getGoodStock(),10,TimeUnit.SECONDS);
//            redisTemplate.opsForList().leftPush("hotList",hotGood);
//        }
//        redisTemplate.expire("hotList", 10, TimeUnit.SECONDS);
        List<Good> hotGoods=goodMapper.getAllGoods();
        for (Good hotGood : hotGoods) {
            redisTemplate.opsForValue().set("hotGood:"+ hotGood.getId(),hotGood,60, TimeUnit.SECONDS);
//            redisTemplate.opsForValue().set("stock:"+hotGood.getId(),hotGood.getGoodStock(),10,TimeUnit.SECONDS);
            redisTemplate.opsForList().leftPush("hotList",hotGood);
        }
        redisTemplate.expire("hotList", 60, TimeUnit.SECONDS);
//        Set<String> keys = redisTemplate.keys("hotGood:*");
//
//        List<Good> allGoods = new ArrayList<>();
//        List<Good> goodsWithTypeId1 = new ArrayList<>();
//        for (String key : keys) {
//            String jsonString=(String)redisTemplate.opsForValue().get(key);
//            ObjectMapper objectMapper = new ObjectMapper();
//            Good good = objectMapper.readValue(jsonString, Good.class);
//       allGoods.add(good);
//        }
//        for (Good good : allGoods) {
//            if (good.getTypeId().equals("1")) {
//                goodsWithTypeId1.add(good);
//            }
//        }

        return ResponseDTO.success(hotGoods);
    }

    @Override
    public ResponseDTO getHotGood(int id) {
        Object o=redisTemplate.opsForValue().get("hotGood"+id);
        return ResponseDTO.success(goodMapper.getHotGood(id));
    }

    @Override
    public ResponseDTO getHotGoodList(int pageNum, int pageSize) {
        int start=(pageNum-1)*pageSize;
        int end=start+pageSize-1;
        List<Object> hotList=redisTemplate.opsForList().range("hotList",start,end);
        return ResponseDTO.success(hotList);
    }
    @Override
    public ResponseDTO getGoodClass() {
        List<GoodClass> goodClass=goodMapper.getGoodClass();
        if(goodClass!=null){
            return ResponseDTO.success(goodClass);
        }
        else{
            return ResponseDTO.error("获取失败");
        }
    }

    @Override
    public ResponseDTO getGoodClassType(String id) {
        List<GoodClassType> goodClassTypes=goodMapper.getGoodClassType(id);
        if(goodClassTypes!=null){
            return ResponseDTO.success(goodClassTypes);
        }
        else{
            return ResponseDTO.error("获取失败");
        }
    }
    @Override
    public ResponseDTO getGoods(String id) throws JsonProcessingException {

//        ResponseDTO dto=getHotGoods();
        Set<String> keys = redisTemplate.keys("hotGood:*");

        List<Good> allGoods = new ArrayList<>();
        List<Good> goodsWithTypeId1 = new ArrayList<>();
        for (String key : keys) {
            String jsonString=(String)redisTemplate.opsForValue().get(key);
            ObjectMapper objectMapper = new ObjectMapper();
            Good good = objectMapper.readValue(jsonString, Good.class);
            allGoods.add(good);
        }
        for (Good good : allGoods) {
            if (good.getTypeId().equals(id)) {
                goodsWithTypeId1.add(good);
            }
        }
        if(allGoods.size()!=0){
            System.out.println("查到了");
            return ResponseDTO.success(goodsWithTypeId1);
        }
        List<Good> goods=goodMapper.getGoods(id);
        if(goods!=null){
            return ResponseDTO.success(goods);
        }
        else{
            return ResponseDTO.error("获取失败");
        }
    }

    @Override
    public ResponseDTO goodToRedis() {
       List<CyGood> cyGoods=goodMapper.goodToRedis();
        for (CyGood cyGood : cyGoods) {
            redisTemplate.opsForValue().set("good:"+ cyGood.getGoodId(),cyGood);
            redisTemplate.opsForValue().set("stock:"+cyGood.getGoodId(),cyGood.getGoodStock());
        }
        return ResponseDTO.success(cyGoods);

    }

    @Override
    public ResponseDTO reduceStorage(String id,String num) {
       int res=goodMapper.reduceStorage(id,num);
       if(res>0){
           return ResponseDTO.success("扣减成功");
       }
       else{
           return ResponseDTO.error("扣减失败");
       }
    }

    @Override
    public ResponseDTO addStorage(String id, String num) {
        int res=goodMapper.addStorage(id,num);
        if(res>0){
            return ResponseDTO.success("增加成功");
        }
        else{
            return ResponseDTO.error("增加失败");
        }
    }

    @Override
    public ResponseDTO generateOrder(String goodId, String userId, String payState, String buyCount, String isTimeOut) {
        int res=goodMapper.generateOrder(goodId,userId,payState,buyCount,isTimeOut);
        if(res>0){
            return ResponseDTO.success("生成成功");
        }
        else{
            return ResponseDTO.error("生成失败");
        }
    }
}
